package jzqb.controller.admin;

import cnki.tpi.base.DataSourceManager;
import haha.life.common.CutPageBean;
import haha.life.common.MonitorTypeTool;
import haha.life.common.PermissionTypeTool;
import haha.life.normal.FileUtil;
import haha.life.normal.FileuploadUtils;
import haha.life.normal.RandomUtils;
import io.swagger.annotations.Api;
import jzqb.annotation.PermissionCheck.PermissionCheck;
import jzqb.bo.AdminQueryBo;
import jzqb.common.Result;
import jzqb.po.CGPo;
import jzqb.po.JSYYPo;
import jzqb.po.YQTJSYYFLPo;
import jzqb.service.AdminCGService;
import jzqb.service.AdminJSYYService;
import jzqb.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/adminJSYY")
@Api(tags = "后台九州影音管理控制层")
public class AdminJSYYController {

    @Autowired
    private AdminJSYYService adminJSYYService;

    @Value("${filePath.fileTemPResource}")
    private String fileTemPResource;

    @Value("${filePath.coverTemPResource}")
    private String coverTemPResource;

    @Value("${filePath.jzyyFilePath}")
    private String jzyyFilePath;

    @Value("${filePath.zjyyCoverPath}")
    private String zjyyCoverPath;

    @Autowired
    private MonitorService monitorService;

    //新建九州影音
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "insertJSYY")
    public Result insertJSYY(@RequestBody JSYYPo jsyyPo,HttpServletRequest request){
        Boolean result = null;
        jsyyPo.setAuthor("");
        result = adminJSYYService.insertJSYY(jsyyPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getAddFile(),jsyyPo.getTitle(), "军事影音库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("新建九洲影音成功");
            }
        }
        return Result.fail("新建九洲影音失败");
    }

    //修改九洲影音
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "updateJSYY")
    public Result updateJSYY(@RequestBody JSYYPo jsyyPo,HttpServletRequest request){
        Boolean result = null;
        jsyyPo.setAuthor("");
        result = adminJSYYService.updateJSYY(jsyyPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getEditFile(),jsyyPo.getTitle(), "军事影音库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("修改九洲影音成功");
            }
        }
        return Result.fail("修改九洲影音失败");
    }

    //删除九洲影音
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "deleteJSYY")
    public Result deleteJSYY(@RequestBody JSYYPo jsyyPo,HttpServletRequest request){
        Boolean result = adminJSYYService.deleteJSYY(jsyyPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getDeleteFile(),jsyyPo.getTitle(), "军事影音库");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("删除九洲影音成功");
            }
        }
        return Result.fail("删除九洲影音失败");
    }

    //查询九洲影音
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @GetMapping(value = "queryJSYY")
    public Result queryJSYY(
            @RequestParam("searchWay") String searchWay,
            @RequestParam("keyWord") String keyWord,
            @RequestParam("classification") String classification,
            @RequestParam("page") int page,
            @RequestParam("pageSize") int pageSize
    ){
        AdminQueryBo bo = new AdminQueryBo();
        bo.setSearchWay(searchWay);
        bo.setKeyWord(keyWord);
        bo.setClassification(classification);
        bo.setStart((page - 1) * pageSize);
        bo.setEnd(pageSize);
        List<JSYYPo> list = adminJSYYService.queryJSYY(bo);
        Integer total = adminJSYYService.count(bo).intValue();
        CutPageBean cutPageBean = new CutPageBean<>();
        cutPageBean.initCutPage(total, pageSize, list);
        return Result.succ(cutPageBean);
    }

    //文件临时上传
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @RequestMapping(value = "/fileTempUpload")
    public Result fileTempUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
        //获取文件名
        String cover = request.getParameter("cover");
        String fileName =file.getOriginalFilename();
        String randDir = RandomUtils.getDirByName(fileName);
        CGPo cgPo = new CGPo();
        cgPo.setDigitFileName(fileName);
        try {
            String filePath = new File(fileTemPResource,randDir).toString();
            FileUtil.saveFile(file, filePath, fileName);//执行保存(文件,目录,文件名)
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(cgPo);
    }

    //图片临时上传
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @RequestMapping(value = "/imageTempUpload")
    public Result imageTempUpload(@RequestParam("file") MultipartFile file) {
        String fileName =file.getOriginalFilename();
        String randDir = RandomUtils.getDirByName(fileName);
        String basePath= coverTemPResource;
        String filePath = new File(basePath,randDir).toString();
        try {
            //执行保存(文件,目录,文件名)
            FileUtil.saveFile(file, filePath, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(fileName);
    }

    //展示九洲影音封面
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @RequestMapping(value ="/getImage",produces="application/json;charset=UTF-8")
    public Result createFolw(HttpServletRequest request, HttpServletResponse response) {
        String cover = request.getParameter("cover");
        String type = request.getParameter("type");
        if("temp".equals(type)){
            type = coverTemPResource;
        }else{
            type = zjyyCoverPath;
        }
        String path = FileUtil.calculateAbstractPath(cover,type);
        byte[] imageBinary = FileuploadUtils.getImageBinary(path);
        return  Result.succ(imageBinary);
    }

    //展示九洲影音视频
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @RequestMapping(value ="/getVideo",produces="application/json;charset=UTF-8")
    public Result getVideo(HttpServletRequest request, HttpServletResponse response) {
        String digitFileName = request.getParameter("digitFileName");
        String type = request.getParameter("type");
        if("temp".equals(type)){
            type = coverTemPResource;
        }else{
            type = jzyyFilePath;
        }
        String path = FileUtil.calculateAbstractPath(digitFileName,type);
        byte[] imageBinary = FileuploadUtils.getImageBinary(path);
        return  Result.succ(imageBinary);
    }

    //查询影音分类
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @GetMapping(value = "queryJSYYFL")
    public Result queryJSYYFL(@RequestParam("keyWord") String keyWord){
        AdminQueryBo bo = new AdminQueryBo();
        bo.setKeyWord(keyWord);
        List<YQTJSYYFLPo> list = adminJSYYService.queryJSYYFL(bo);
        return Result.succ(list);
    }

    //新建影音分类
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "insertJSYYFL")
    public Result insertJSYYFL(@RequestBody YQTJSYYFLPo YQTJSYYFLPo,HttpServletRequest request){
        Result result = adminJSYYService.insertJSYYFL(YQTJSYYFLPo);
        if(result.getCode()==200){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getAddFL(),YQTJSYYFLPo.getShowName());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("新建九洲影音分类成功");
            }
        }
        if(result.getCode() == 500){
            return Result.fail(result.getMsg());
        }

        return Result.fail("新建九洲影音分类失败");
    }

    //修改影音分类
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "updateJSYYFL")
    public Result updateJSYYFL(@RequestBody YQTJSYYFLPo YQTJSYYFLPo,HttpServletRequest request){
        Result result = adminJSYYService.updateJSYYFL(YQTJSYYFLPo);
        if(result.getCode()==200){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getEditFL(),YQTJSYYFLPo.getShowName());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("修改九洲影音分类成功");
            }
        }
        if(result.getCode() == 500){
            return Result.fail(result.getMsg());
        }
        return Result.fail("修改九洲影音分类失败");
    }

    //删除影音分类
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @PostMapping(value = "deleteJSYYFL")
    public Result deleteJSYYFL(@RequestBody YQTJSYYFLPo YQTJSYYFLPo,HttpServletRequest request){
        Boolean result = null;
        result = adminJSYYService.deleteJSYYFL(YQTJSYYFLPo);
        if(result){
            Boolean insertRes = true;
            try {
                insertRes = monitorService.record(request,new MonitorTypeTool().getDeleteFL(),YQTJSYYFLPo.getShowName());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            if(insertRes){
                return Result.succ("删除九洲影音分类成功");
            }
        }
        return Result.fail("删除九洲影音分类失败");
    }

    //查询影音分类最大排序
    @PermissionCheck(permissionTypeList  ={PermissionTypeTool.business_mgt})
    @GetMapping(value = "queryJSYYFLMax")
    public Result queryJSYYFLMax(){
        List<YQTJSYYFLPo> max = adminJSYYService.queryJSYYFLMax();
        String maxId = "0";
        if(max != null && !max.isEmpty()){
            maxId = max.get(0).getSortNumber();
        }
        return Result.succ(maxId);
    }
}

